# coding: utf-8
from datetime import datetime

from abstract import base
from config import settings
from app import model
from app.helper import util
#public


#action
class admin_index(base):
    def get(self):
        page = self.get_argument("page",1)
        adminlist=model.admin.list(page)
        self.render(adminlist = adminlist)

class admin_edit(base):
    def get(self):
        i = self.input(admin_id=0,username="",password="")
        i.admin_id=util.get_int(i.admin_id,0)
        if i.admin_id>0:
           i=model.admin.find_one(i.admin_id)
       
        self.render(admin=i)
    
    def post(self):
        i=self.input(admin_id=0,username="",password="",created=datetime.now())
        i.admin_id=util.get_int(i.admin_id,0)
        i.password=util.sha1(i.password)

        #数据验证
        errors={}
        if model.admin.check_username(i.admin_id,i.username):
           errors['check_username']=u"用户名已经存在"

        if errors:
           return self.redirect('/admin/admin')
       
        model.admin.save(i)
        
        if i.admin_id>0:
           return self.redirect('/admin/admin')
        else:
           return self.redirect('/admin/admin/edit')
        
        
